package com.jwq.bsdw.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwq.bsdw.mapper.BlogMapper;
import com.jwq.bsdw.pojo.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by jinwenqi
 */
@Service
@SuppressWarnings("ALL")
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;



    @Override
    public Blog getAndConvert(Long id) {
        return null;
    }



    @Override
    public Integer saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        return blogMapper.insert(blog);
    }

    @Override
    public Integer updateBlog(Long id, Blog blog) {
        blog.setUpdateTime(new Date());
        return blogMapper.updateById(blog);
    }


    @Override
    public IPage findByPage(Integer pageNum, Integer pageSize) {
        IPage p=new Page(pageNum,pageSize,true);
        IPage page = blogMapper.selectPage(p, null);
        return page;
    }




    //编辑器转换为html
/*    @Transactional
    @Override
    public Blog getAndConvert(Long id) {
        Blog blog = blogRepository.findOne(id);
        if (blog == null) {
            throw new NotFoundException("该博客不存在");
        }
        Blog b = new Blog();
        BeanUtils.copyProperties(blog, b);
        String content = b.getContent();
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
       blogRepository.updateViews(id);
        return b;
    }*/


    /**
     * 首页推荐展示
     *
     * @param size
     * @return
     */
   /* @Override
    //@Cacheable(cacheNames = "recommend",key = "456")
    public List<Blog> listRecommendBlogTop(Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        Pageable pageable = new PageRequest(0, size, sort);
        return blogRepository.findTop(pageable);
    }*/

   /* @Override
    public Map<String, List<Blog>> archiveBlog() {
        List<String> years = blogRepository.findGroupYear();
        Map<String, List<Blog>> map = new HashMap<>();
        for (String year : years) {
            map.put(year, blogRepository.findByYear(year));
        }
        return map;
    }*/

}
